const userInfoService = require('./../services/user-info')
const userCode = require('./../codes/user')
const dbUtils = require('./../utils/db-util')
var jwt = require('jsonwebtoken');

module.exports = {

  /**
   * 登录操作
   * @param  {obejct} ctx 上下文对象
   */
  async signIn( ctx ) {
    let formData = ctx.request.body.login
    let result = {
      success: false,
      message: '',
      data: null,
      code: '',
      token: '',
      roles: 'user'
    }

    let userResult = await userInfoService.signIn( formData )
    if ( userResult ) {
      if (userResult.stu_name == 'admin'){
        result.roles = 'admin'
      }
      if ( formData.stuName === userResult.stu_name ) {
        result.success = true
        result.code = 20000
      } else {
        result.message = userCode.FAIL_USER_NAME_OR_PASSWORD_ERROR
        result.code = 'FAIL_USER_NAME_OR_PASSWORD_ERROR'
      }
    } else {
      result.code = 'FAIL_USER_NO_EXIST',
      result.message = userCode.FAIL_USER_NO_EXIST
    }

    if ( result.success === true ) {
      const userToken = {
        name: userResult.stu_name,
        phone: userResult.stu_phone,
      }
      const token = jwt.sign({
        exp: Math.floor(Date.now() / 1000) + (60 * 60),
        data: userToken
      }, 'stuhelp')
      result.token = token
      ctx.body = result
    } else {
      ctx.body = result
    }
  },

  /**
   * 注册操作
   * @param   {obejct} ctx 上下文对象
   */
  async signUp( ctx ) {
    let formData = ctx.request.body.form
    let result = {
      success: false,
      message: '',
      data: null,
      code: null
    }

    let validateResult = userInfoService.validatorSignUp( formData )

    if ( validateResult.success === false ) {
      result = validateResult
      ctx.body = result
      return
    }

    let existOne  = await userInfoService.getExistOne(formData)

    if ( existOne  ) {
      if ( existOne .stu_phone === formData.phone ) {
        result.message = userCode.FAIL_USER_NAME_IS_EXIST
        ctx.body = result
        return
      }
      if ( existOne .stu_name === formData.name ) {
        result.message = userCode.FAIL_EMAIL_IS_EXIST
        ctx.body = result
        return
      }
    }


    let userResult = await userInfoService.create({
      stu_phone: formData.phone,
      password: formData.password,
      stu_name: formData.username,
    })


    if ( userResult && userResult.insertId * 1 > 0) {
      result.success = true
      result.message = userCode.Success_Add_User
      result.code = 20000
    } else {
      result.message = userCode.ERROR_SYS
    }

    ctx.body = result
  },

  /**
   * 获取用户信息
   * @param    {obejct} ctx 上下文对象
   */
  async getLoginUserInfo( ctx ) {
    let result = {
      success: false,
      message: '',
      data: null,
      roles:[
        "admin"
      ],
    }
    const token = ctx.request.body.token
    var decoded = jwt.decode(token);
    const userName = decoded.data.name
    let userInfo = await userInfoService.getUserInfoByUserName( userName )
    if ( userInfo ) {
      result.data = userInfo
      result.success = true
      result.code = 20000
      result.message = '登陆中...'
      result.avatar = "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif"
    } else {
      result.message = userCode.FAIL_USER_NO_LOGIN
    }
    ctx.body = result
  },

  /**
   * 
   * @param {获取所有用户信息} ctx 
   */
  async getAllUsers( ctx ) {
    let result = {
      success: false,
      message: '',
      data: null,
    }
    let userData = await userInfoService.getAllUserData()
    console.log(userData)
    if ( userData.length > 0 ) {
      result.data = userData
      result.message = '加载成功'
      result.code = 20000
    } else {
      result.data = ''
      result.message = '暂无数据'
      result.code = 20000
    }
    ctx.body = result
  },

  /**
   * 
   * @param {删除用户} ctx 
   */ 
  async DelUser( ctx ) {
    let result = {
      success: false,
      message: '',
      data: null,
    }
    let name = ctx.request.body.name
    let userData = await userInfoService.DelUsers(name)
    if (userData) {
      result.success =  true,
      result.message = '删除成功'
      result.code = 20000
    } else {
      result.message = '操作失败'
      result.code = 20000
    }
    ctx.body = result
  },
  /**
   * 
   * @param {更改个人名言} ctx 
   */
  async ChangeAbout( ctx ) {
    let result = {
      success: false,
      message: '',
      data: null,
    }
    console.log(ctx.request.body)
    let stuAbout = ctx.request.body.stuAbout
    let name = ctx.request.body.name
    let userData = await dbUtils.updateData('users', stuAbout, name)
    if (userData){
      result.success = true
      result.code = 20000
      result.message = '修改成功'
    } else {
      result.success = false
      result.code = 20000
      result.message = '修改失败'
    }
    ctx.body = result
  },

  /**
   * 
   * @param {注销用户} ctx 
   */
  logout( ctx ) {
    let result = {
      success: false,
      message: '',
      code: 40000
    }
    result.success = true
    result.message = '注销成功'
    result.code = 20000
    ctx.body = result
  }
}
